home *** CD-ROM | disk | FTP | other *** search
/ Your Choice 3 / Your Choice Software Collection 3.iso / prgmming / flames / flame_ac.c < prev    next >
C/C++ Source or Header  |  1993-08-29  |  10KB  |  257 lines

  1. //**************************************************************************
  2. //*                                                                        *
  3. //*    CFLAMES1 by Kirk A. Baum                                            *
  4. //*       This C program was addapted from a pascal program by M.D.Mackey. *
  5. //*       It has been modified to half the resolution used by Mark and the *
  6. //*       routines are entirely in C.  The original code was written in    *
  7. //*       Pascal with Inline assemby.  As with Marks code, I release this  *
  8. //*       code into the public domain.  It may be freely distributed and   *
  9. //*       modified, but please give credit where it is due if used.        *
  10. //*       If you have any suggestions or comments please contact me at:    *
  11. //*       kbaum@Novell.com.                                                *
  12. //*       Mark can be reached at: mackey@aqueous.ml.csiro.au               *
  13. //*                                                                        *
  14. //**************************************************************************
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <time.h>
  19. #include <conio.h>
  20. #include <dos.h>
  21.  
  22.  
  23. #define VIDEO_INT       0x10
  24. #define VGA_MODE        0x13
  25. #define TEXT_MODE       0x03
  26. #define V_WIDTH         80
  27. #define V_HEIGHT        50
  28. #define BUF_WIDTH       80
  29. #define BUF_HEIGHT      56
  30. #define PALETTE_SIZE    256*3
  31. #define INT_SIZE        2
  32. #define ESC_KEY         27
  33. #define VGA_SCREEN      0xa000
  34. #define DAC_WRITE_INDEX 0x03c8
  35. #define DAC_DATA_REG    0x03c9
  36.  
  37. void SetVGAMode13(void);
  38. void RestoreTextMode(void);
  39. void SetPalette(void);
  40.  
  41.  
  42. unsigned char far *screen;    // pointer to the VGA graphics screen 320x200
  43. int p1[BUF_HEIGHT][BUF_WIDTH];// VGA buffer, quarter resolution w/extra lines
  44. unsigned int i,j,k,l,delta;   // looping variables, counters, and data
  45. char ch;                      // input character
  46.  
  47. // VGA color palette for flames
  48. unsigned char pal[PALETTE_SIZE]={  0,  0,  0,  0,  0, 24,  0,  0, 24,  0,  0, 28,
  49.                    0,  0, 32,  0,  0, 32,  0,  0, 36,  0,  0, 40,
  50.                    8,  0, 40, 16,  0, 36, 24,  0, 36, 32,  0, 32,
  51.                   40,  0, 28, 48,  0, 28, 56,  0, 24, 64,  0, 20,
  52.                   72,  0, 20, 80,  0, 16, 88,  0, 16, 96,  0, 12,
  53.                  104,  0,  8,112,  0,  8,120,  0,  4,128,  0,  0,
  54.                  128,  0,  0,132,  0,  0,136,  0,  0,140,  0,  0,
  55.                  144,  0,  0,144,  0,  0,148,  0,  0,152,  0,  0,
  56.                  156,  0,  0,160,  0,  0,160,  0,  0,164,  0,  0,
  57.                  168,  0,  0,172,  0,  0,176,  0,  0,180,  0,  0,
  58.                  184,  4,  0,188,  4,  0,192,  8,  0,196,  8,  0,
  59.                  200, 12,  0,204, 12,  0,208, 16,  0,212, 16,  0,
  60.                  216, 20,  0,220, 20,  0,224, 24,  0,228, 24,  0,
  61.                  232, 28,  0,236, 28,  0,240, 32,  0,244, 32,  0,
  62.                  252, 36,  0,252, 36,  0,252, 40,  0,252, 40,  0,
  63.                  252, 44,  0,252, 44,  0,252, 48,  0,252, 48,  0,
  64.                  252, 52,  0,252, 52,  0,252, 56,  0,252, 56,  0,
  65.                  252, 60,  0,252, 60,  0,252, 64,  0,252, 64,  0,
  66.                  252, 68,  0,252, 68,  0,252, 72,  0,252, 72,  0,
  67.                  252, 76,  0,252, 76,  0,252, 80,  0,252, 80,  0,
  68.                  252, 84,  0,252, 84,  0,252, 88,  0,252, 88,  0,
  69.                  252, 92,  0,252, 96,  0,252, 96,  0,252,100,  0,
  70.                  252,100,  0,252,104,  0,252,104,  0,252,108,  0,
  71.                  252,108,  0,252,112,  0,252,112,  0,252,116,  0,
  72.                  252,116,  0,252,120,  0,252,120,  0,252,124,  0,
  73.                  252,124,  0,252,128,  0,252,128,  0,252,132,  0,
  74.                  252,132,  0,252,136,  0,252, 136,   0,252, 140,   0,
  75.                  252, 140,   0,252, 144,   0,252, 144,   0,252, 148,   0,
  76.                  252, 152,   0,252, 152,   0,252, 156,   0,252, 156,   0,
  77.                  252, 160,   0,252, 160,   0,252, 164,   0,252, 164,   0,
  78.                  252, 168,   0,252, 168,   0,252, 172,   0,252, 172,   0,
  79.                  252, 176,   0,252, 176,   0,252, 180,   0,252, 180,   0,
  80.                  252, 184,   0,252, 184,   0,252, 188,   0,252, 188,   0,
  81.                  252, 192,   0,252, 192,   0,252, 196,   0,252, 196,   0,
  82.                  252, 200,   0,252, 200,   0,252, 204,   0,252, 208,   0,
  83.                  252, 208,   0,252, 208,   0,252, 208,   0,252, 208,   0,
  84.                  252, 212,   0,252, 212,   0,252, 212,   0,252, 212,   0,
  85.                  252, 216,   0,252, 216,   0,252, 216,   0,252, 216,   0,
  86.                  252, 216,   0,252, 220,   0,252, 220,   0,252, 220,   0,
  87.                  252, 220,   0,252, 224,   0,252, 224,   0,252, 224,   0,
  88.                  252, 224,   0,252, 228,   0,252, 228,   0,252, 228,   0,
  89.                  252, 228,   0,252, 228,   0,252, 232,   0,252, 232,   0,
  90.                  252, 232,   0,252, 232,   0,252, 236,   0,252, 236,   0,
  91.                  252, 236,   0,252, 236,   0,252, 240,   0,252, 240,   0,
  92.                  252, 240,   0,252, 240,   0,252, 240,   0,252, 244,   0,
  93.                  252, 244,   0,252, 244,   0,252, 244,   0,252, 248,   0,
  94.                  252, 248,   0,252, 248,   0,252, 248,   0,252, 252,   0,
  95.                  252, 252,   4,252, 252,   8,252, 252,  12,252, 252,  16,
  96.                  252, 252,  20,252, 252,  24,252, 252,  28,252, 252,  32,
  97.                  252, 252,  36,252, 252,  40,252, 252,  40,252, 252,  44,
  98.                  252, 252,  48,252, 252,  52,252, 252,  56,252, 252,  60,
  99.                  252, 252,  64,252, 252,  68,252, 252,  72,252, 252,  76,
  100.                  252, 252,  80,252, 252,  84,252, 252,  84,252, 252,  88,
  101.                  252, 252,  92,252, 252,  96,252, 252, 100,252, 252, 104,
  102.                  252, 252, 108,252, 252, 112,252, 252, 116,252, 252, 120,
  103.                  252, 252, 124,252, 252, 124,252, 252, 128,252, 252, 132,
  104.                  252, 252, 136,252, 252, 140,252, 252, 144,252, 252, 148,
  105.                  252, 252, 152,252, 252, 156,252, 252, 160,252, 252, 164,
  106.                  252, 252, 168,252, 252, 168,252, 252, 172,252, 252, 176,
  107.                  252, 252, 180,252, 252, 184,252, 252, 188,252, 252, 192,
  108.                  252, 252, 196,252, 252, 200,252, 252, 204,252, 252, 208,
  109.                  252, 252, 208,252, 252, 212,252, 252, 216,252, 252, 220,
  110.                  252, 252, 224,252, 252, 228,252, 252, 232,252, 252, 236,
  111.                  252, 252, 240,252, 252, 244,252, 252, 248,252, 252, 252};
  112.  
  113.  
  114.  
  115. //************************************************************************
  116. //*  SetVGAMode13                                                        *
  117. //*     Sets the graphics screen to mode 13h (320x200) at 256 colors.    *
  118. //************************************************************************
  119. void SetVGAMode13()
  120. {
  121. union REGS regs;
  122.  
  123. regs.x.ax = VGA_MODE;
  124. int86(VIDEO_INT, ®s, ®s);
  125. }
  126.  
  127.  
  128. //************************************************************************
  129. //*  RestoreTextMode                                                     *
  130. //*     Sets the video mode back to text mode.                           *
  131. //************************************************************************
  132. void RestoreTextMode()
  133. {
  134. union REGS regs;
  135.  
  136. regs.x.ax = TEXT_MODE;
  137. int86(VIDEO_INT, ®s, ®s);
  138. }
  139.  
  140. //************************************************************************
  141. //*  SetPalette                                                          *
  142. //*    Sets the 256 color palette of the VGA to reflect the RGB colos    *
  143. //*    contained in the pal array.  Set the DAC_WRITE_INDEX to the color *
  144. //*    you want to start with, in our case 0 (the first color index).    *
  145. //*    Then send the RGB values for each successive color index to the   *
  146. //*    DAC_DATA_REGister.
  147. //************************************************************************
  148. void SetPalette()
  149. {
  150. int  i;
  151.  
  152. outportb(DAC_WRITE_INDEX, 0);
  153. for(i=0; i<PALETTE_SIZE; i++)
  154.    {
  155.    pal[i] = pal[i] >> 2;
  156.    outportb(DAC_DATA_REG, pal[i]);
  157.    }
  158. }
  159.  
  160.  
  161. //************************************************************************
  162. //*  Main                                                                *
  163. //*    Creates a fire (plasma) effect                                    *
  164. //************************************************************************
  165. void main()
  166. {
  167.   SetVGAMode13();      // Setup the graphics mode
  168.   SetPalette();        // Setup the color palette
  169.   randomize();         // Initialize the random number generator
  170.   ch=' ';              // Initialize the input character
  171.  
  172.  
  173.   // Initialize the video buffer to 0's
  174.   for(i=0;i<BUF_HEIGHT;i++)
  175.      {
  176.      for(j=0;j<BUF_WIDTH;j++)
  177.     {
  178.     p1[i][j]=0;
  179.     }
  180.      }
  181.  
  182.   // Loop until ESC is pressed
  183.   while(ch != ESC_KEY)
  184.     {
  185.  
  186.     // Transform current buffer
  187.     for(i=1;i<BUF_HEIGHT;i++)
  188.        {
  189.        for(j=0;j<BUF_WIDTH;j++)
  190.       {
  191.       if(j==0) p1[i-1][j] = (p1[i][j]+
  192.                  p1[i-1][79]+
  193.                  p1[i][j+1]+
  194.                  p1[i+1][j]) / 4;
  195.       else if(j==79) p1[i-1][j] = (p1[i][j]+
  196.                        p1[i][j-1]+
  197.                        p1[i+1][0]+
  198.                        p1[i+1][j]) / 4;
  199.       else p1[i-1][j] = (p1[i][j]+
  200.                  p1[i][j-1]+
  201.                  p1[i][j+1]+
  202.                  p1[i+1][j]) / 4;
  203.  
  204.  
  205.       //if(p1[i][j] > 0) p1[i][j]= p1[i][j] - 1;
  206.       //if(p1[i][j] > 0) p1[i][j]= p1[i][j] - 1;
  207.       //if(p1[i][j] > 0) p1[i][j]= p1[i][j] - 1;
  208.  
  209.       if(p1[i][j] > 11) p1[i][j]= p1[i][j] - 12;
  210.       else if(p1[i][j] > 3) p1[i][j] = p1[i][j] - 4;
  211.       else {
  212.            if(p1[i][j] > 0) p1[i][j]--;
  213.            if(p1[i][j] > 0) p1[i][j]--;
  214.            if(p1[i][j] > 0) p1[i][j]--;
  215.            }
  216.       }
  217.        }
  218.  
  219.     // Set new bottom line with random white or black
  220.     delta = 0;
  221.     for(j=0;j<BUF_WIDTH;j++)
  222.        {
  223.        if(random(10) < 5)
  224.      {
  225.      delta=random(2)*255;
  226.      }
  227.        p1[BUF_HEIGHT - 2][j] = delta;
  228.        p1[BUF_HEIGHT - 1][j] = delta;
  229.        }
  230.  
  231.     // Write the buffer to the screen
  232.     screen = MK_FP(VGA_SCREEN,0);
  233.     for(i=0; i<V_HEIGHT; i++)
  234.        {
  235.        for(k=0; k<4; k++)
  236.       {
  237.       for(j=0; j<V_WIDTH; j++)
  238.          {
  239.          for(l=0; l<4; l++)
  240.         {
  241.         *screen = (unsigned char)p1[i][j];
  242.         screen++;
  243.         }
  244.          }
  245.       }
  246.        }
  247.  
  248.     // Check for ESC key pressed
  249.     if(kbhit())
  250.       {
  251.       ch=getch();
  252.       }
  253.  
  254.     }//End of While
  255.  
  256.   RestoreTextMode();
  257. }